﻿using csLTDMC;
using DMC300Axis_IO.Function;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using OCR.SystemThread;




namespace OCR
{
    public partial class Param_Form : Form
    {
        public Param_Form()
        {
            InitializeComponent();
        }

        private void Param_Form_Load(object sender, EventArgs e)
        {
            timer1.Start();
        }
        //NG吹气
        private void BlowBtn_Click(object sender, EventArgs e)
        {
            NG_Control.iNG_Step = 10;
        }
        //取消吹气
        private void CancelBlowBtn_Click(object sender, EventArgs e)
        {
            NG_Control.iNG_Step = 0;
        }
        //NG参数储存
        private void NGStorageBtn_Click(object sender, EventArgs e)
        {
            DMC300_Info.NGDelay = ushort.Parse(NGDelayTb.Text);
            DMC300_Info.NG_Action = ushort.Parse(NG_ActionTb.Text);

            if (NG_ContinuousCb.Checked) DMC300_Info.NG_Continuous = true;
            else DMC300_Info.NG_Continuous = false;
        }
        //触发取像参数储存
        private void GrabStorageBtn_Click(object sender, EventArgs e)
        {
            DMC300_Info.CameraTrigger_Delay = ushort.Parse(CameraTrigger_DelayTb.Text);  //一將相機外部觸發延遲時間設定進去
            DMC300_Info.CameraTrigger_Time = ushort.Parse(CameraTrigger_TimeTb.Text);   //一將相機外部觸發作動時間設定進去
        }
        //是否启动触发流程
        private void StartGrabCb_CheckedChanged(object sender, EventArgs e)
        {
            if (StartGrabCb.Checked)
            {
                CameraTriggerThread.iCameraTrigger_Step = 10;
                DMC300_Info.CameraTrigger_IsStop = true;
            }
            else
            {
                //CameraTriggerThread.iCameraTrigger_Step = 0;
                DMC300_Info.CameraTrigger_IsStop = false;
            }
        }
        //退出测试
        private void QuitParamBtn_Click(object sender, EventArgs e)
        {
            if (LTDMC.dmc_read_sevon_pin(DMC300_Info.usCardId[0], 0) == 0)          //當伺服始能輸出狀態就進行關閉
            {
                LTDMC.dmc_write_sevon_pin(DMC300_Info.usCardId[0], 0, 1);
            }
            if (LTDMC.dmc_read_sevon_pin(DMC300_Info.usCardId[0], 1) == 0)          //當伺服始能輸出狀態就進行關閉
            {
                LTDMC.dmc_write_sevon_pin(DMC300_Info.usCardId[0], 1, 1);
            }
            DIO_Thread.DIORun = false;                                              //關閉DIO線程
            NG_Control.NG_Flowrec = false;                                          //關閉踢料線程
            CameraTriggerThread.CameraTrigger_Flowrec = false;                      //關閉觸發取像線程

            LTDMC.nmc_set_connect_state(DMC300_Info.usCardId[0], decimal.ToUInt16(numericUpDown1.Value), 0, 0);//设置CAN通讯状态，断开
            LTDMC.dmc_board_close();
            this.Close();
        }

        //速度，位置状态清除
        private void StateClearBtn_Click(object sender, EventArgs e)
        {
            ushort usAxis = 0;                           //轴号
            int dPos = 0;                                     // 当前位置
            LTDMC.dmc_set_position(DMC300_Info.usCardId[0], usAxis, dPos); //设置指定轴的当前指令位置值歸零
            LTDMC.dmc_set_encoder(DMC300_Info.usCardId[0], usAxis, dPos);  //設置指定軸編碼器歸零
        }
        //执行速度变换
        private void ChangeSpeedBtn_Click(object sender, EventArgs e)
        {
            ushort usAxis = 0; //轴号
            double dNewVel = Double.Parse(changeSpeedTb.Text);                // 新的运行速度
            double dTaccDec = Double.Parse(changeTimeTb.Text);              //变速时间
            LTDMC.dmc_change_speed(DMC300_Info.usCardId[0], usAxis, dNewVel, dTaccDec);  //在线变速
        }
        //伺服始能
        private void ServoStartBtn_Click(object sender, EventArgs e)
        {
            if (LTDMC.dmc_read_sevon_pin(DMC300_Info.usCardId[0], 0) == 1)      //當讀取狀態為1時，當下始能未開啟
            {
                LTDMC.dmc_write_sevon_pin(DMC300_Info.usCardId[0], 0, 0);       //下達0時是開啟始能
            }
            else
            {
                LTDMC.dmc_write_sevon_pin(DMC300_Info.usCardId[0], 0, 1);       ////下達1時是關閉始能
            }
        }
        //连续运动
        private void ContinuousMotionBtn_Click(object sender, EventArgs e)
        {
            ushort usAxis = 0; //轴号
            double dStartVel;           //起始速度
            double dMaxVel;             //运行速度
            double dTacc;               //加速时间
            double dTdec;               //减速时间
            double dStopVel;            //停止速度
            double dSpara;              //S段时间       
            ushort usDir;               //运动方向

            dStartVel = Double.Parse(startSpeedTb.Text);
            dMaxVel = Double.Parse(runSpeedTb.Text);
            dTacc = Double.Parse(upTimeTb.Text);
            dTdec = Double.Parse(downTimeTb.Text);
            dStopVel = Double.Parse(stopSpeedTb.Text);
            dSpara = Double.Parse(S_TimeTb.Text);

            if (forwardRb.Checked)
            {
                usDir = 1;
            }
            else
            {
                usDir = 0;
            }

            LTDMC.dmc_set_profile(DMC300_Info.usCardId[0], usAxis, dStartVel, dMaxVel, dTacc, dTdec, dStopVel);//设置速度参数
            LTDMC.dmc_set_s_profile(DMC300_Info.usCardId[0], usAxis, 0, dSpara);                                    //设置S段速度参数
            LTDMC.dmc_vmove(DMC300_Info.usCardId[0], usAxis, usDir);                                                  //定速运动
        }

        //轴停止
        private void stopAxisBtn_Click(object sender, EventArgs e)
        {
            ushort usAxis = 0;                             //轴号
            ushort usStopMode = 0;                                 //制动方式，0：减速停止，1：紧急停止
            double dTdec = Double.Parse(downTimeTb.Text);       //减速时间         
            LTDMC.dmc_set_dec_stop_time(DMC300_Info.usCardId[0], usAxis, dTdec);  //设置减速停止时间
            LTDMC.dmc_stop(DMC300_Info.usCardId[0], usAxis, usStopMode);
        }
        //运转一圈
        private void circleOnceBtn_Click(object sender, EventArgs e)
        {
            ushort usAxis = 0; //轴号
            double dStartVel;           //起始速度
            double dMaxVel;             //运行速度
            double dTacc;               //加速时间
            double dTdec;               //减速时间
            double dStopVel;            //停止速度
            double dSpara;              //S段时间       
            ushort usDir;               //运动方向            
            int nDist;               //目标位置
            ushort usPosiMode;          //运动模式0：相对坐标模式，1：绝对坐标模式

            dStartVel = Double.Parse(startSpeedTb.Text);
            dMaxVel = Double.Parse(runSpeedTb.Text);
            dTacc = Double.Parse(upTimeTb.Text);
            dTdec = Double.Parse(downTimeTb.Text);
            dStopVel = Double.Parse(stopSpeedTb.Text);
            dSpara = Double.Parse(S_TimeTb.Text);
            nDist = int.Parse(motorCircleTb.Text);
            if (forwardRb.Checked)
            {
                usDir = 1;
            }
            else
            {
                usDir = 0;
            }

            usPosiMode = 0;

            LTDMC.dmc_set_profile(DMC300_Info.usCardId[0], usAxis, dStartVel, dMaxVel, dTacc, dTdec, dStopVel);//设置速度参数
            LTDMC.dmc_set_s_profile(DMC300_Info.usCardId[0], usAxis, 0, dSpara);                               //设置S段速度参数
            LTDMC.dmc_pmove(DMC300_Info.usCardId[0], usAxis, nDist, usPosiMode);                               //定长运动
        }
        //DO全部取消输出
        private void cancleAllDOBtn_Click(object sender, EventArgs e)
        {
            for (ushort i = 0; i < 16; i++)
            {
                DIO_Thread.Do[i] = 0;
                foreach (Control ctemp in AllDOGb.Controls)
                {
                    if (ctemp.Name == "checkBox" + i.ToString())
                    {
                        ((System.Windows.Forms.CheckBox)ctemp).Checked = false;
                    }
                }
            }
        }
        //DO全部输出
        private void allDOBtn_Click(object sender, EventArgs e)
        {
            for (ushort i = 0; i < 16; i++)
            {
                DIO_Thread.Do[i] = 1;
                foreach (Control ctemp in AllDOGb.Controls)
                {
                    if (ctemp.Name == "checkBox" + i.ToString())
                    {
                        ((CheckBox)ctemp).Checked = true;
                    }
                }
            }
        }
        //CAN连接
        private void canConnectBtn_Click(object sender, EventArgs e)
        {
            ushort usCardNm = 0;
            ushort usCardstate = 0;
            short err = LTDMC.nmc_get_connect_state(DMC300_Info.usCardId[0], ref usCardNm, ref usCardstate);
            if (err != 0)
            {
                if (usCardNm == 1 & usCardstate == 0)
                {
                    LTDMC.nmc_set_connect_state(DMC300_Info.usCardId[0], decimal.ToUInt16(numericUpDown1.Value), 1, 0);//设置CAN通讯状态，连接
                }
            }
            else
            {
                MessageBox.Show("連接IO擴充失敗");
            }
        }
        //CAN断开
        private void canBreakBtn_Click(object sender, EventArgs e)
        {
            LTDMC.nmc_set_connect_state(DMC300_Info.usCardId[0], decimal.ToUInt16(numericUpDown1.Value), 0, 0);//设置CAN通讯状态，断开
        }

        private void checkBox0_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox0.Checked)
            {
                DIO_Thread.Do[0] = 1;
            }
            else
            {
                DIO_Thread.Do[0] = 0;
            }
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked)
            {
                DIO_Thread.Do[1] = 1;
            }
            else
            {
                DIO_Thread.Do[1] = 0;
            }
        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox2.Checked)
            {
                DIO_Thread.Do[2] = 1;
            }
            else
            {
                DIO_Thread.Do[2] = 0;
            }
        }

        private void checkBox3_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox3.Checked)
            {
                DIO_Thread.Do[3] = 1;
            }
            else
            {
                DIO_Thread.Do[3] = 0;
            }
        }

        private void checkBox4_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox4.Checked)
            {
                DIO_Thread.Do[4] = 1;
            }
            else
            {
                DIO_Thread.Do[4] = 0;
            }
        }

        private void checkBox5_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox5.Checked)
            {
                DIO_Thread.Do[5] = 1;
            }
            else
            {
                DIO_Thread.Do[5] = 0;
            }
        }

        private void checkBox6_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox6.Checked)
            {
                DIO_Thread.Do[6] = 1;
            }
            else
            {
                DIO_Thread.Do[6] = 0;
            }
        }

        private void checkBox7_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox7.Checked)
            {
                DIO_Thread.Do[7] = 1;
            }
            else
            {
                DIO_Thread.Do[7] = 0;
            }
        }

        private void checkBox8_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox8.Checked)
            {
                DIO_Thread.Do[8] = 1;
            }
            else
            {
                DIO_Thread.Do[8] = 0;
            }
        }

        private void checkBox9_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox9.Checked)
            {
                DIO_Thread.Do[9] = 1;
            }
            else
            {
                DIO_Thread.Do[9] = 0;
            }
        }

        private void checkBox10_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox10.Checked)
            {
                DIO_Thread.Do[10] = 1;
            }
            else
            {
                DIO_Thread.Do[10] = 0;
            }
        }

        private void checkBox11_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox11.Checked)
            {
                DIO_Thread.Do[11] = 1;
            }
            else
            {
                DIO_Thread.Do[11] = 0;
            }
        }

        private void checkBox12_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox12.Checked)
            {
                DIO_Thread.Do[12] = 1;
            }
            else
            {
                DIO_Thread.Do[12] = 0;
            }
        }

        private void checkBox13_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox13.Checked)
            {
                DIO_Thread.Do[13] = 1;
            }
            else
            {
                DIO_Thread.Do[13] = 0;
            }
        }

        private void checkBox14_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox14.Checked)
            {
                DIO_Thread.Do[14] = 1;
            }
            else
            {
                DIO_Thread.Do[14] = 0;
            }
        }

        private void checkBox15_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox15.Checked)
            {
                DIO_Thread.Do[15] = 1;
            }
            else
            {
                DIO_Thread.Do[15] = 0;
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            ushort usAxis = 0;  //轴号
            double dCurrentVel = 0;     //速度
            int nCmdPos = 0;         //指令位置
            int nEnPos = 0;          //编码器反馈位置

            dCurrentVel = LTDMC.dmc_read_current_speed(DMC300_Info.usCardId[0], usAxis); // 读取轴当前速度
            currSpeed.Text = dCurrentVel.ToString();
            nCmdPos = LTDMC.dmc_get_position(DMC300_Info.usCardId[0], usAxis);           //读取指定轴指令位置值
            currLocationTb.Text = nCmdPos.ToString();
            nEnPos = LTDMC.dmc_get_encoder(DMC300_Info.usCardId[0], usAxis);             //读取指定轴的编码器反馈值
            feedBackTb.Text = nEnPos.ToString();

            if (LTDMC.dmc_check_done(DMC300_Info.usCardId[0], usAxis) == 0)                      //读取指定轴运动状态
            {
                runStateTb.Text = "运行中";
            }
            else
            {
                runStateTb.Text = "停止中";
            }
            //////////////////////////////////////////////////////////////////
            //ushort usAxis_1 = 1;  //轴号
            //double dCurrentVel_1 = 0;     //速度
            //int nCmdPos_1 = 0;         //指令位置
            //int nEnPos_1 = 0;          //编码器反馈位置

            //dCurrentVel_1 = LTDMC.dmc_read_current_speed(DMC300_Info.usCardId[0], usAxis_1); // 读取轴当前速度
            //textBox16.Text = dCurrentVel_1.ToString();
            //nCmdPos_1 = LTDMC.dmc_get_position(DMC300_Info.usCardId[0], usAxis_1);           //读取指定轴指令位置值
            //textBox58.Text = nCmdPos_1.ToString();
            //nEnPos_1 = LTDMC.dmc_get_encoder(DMC300_Info.usCardId[0], usAxis_1);             //读取指定轴的编码器反馈值
            //textBox15.Text = nEnPos_1.ToString();

            //if (LTDMC.dmc_check_done(DMC300_Info.usCardId[0], usAxis_1) == 0)                      //读取指定轴运动状态
            //{
            //    textBox14.Text = "运行中";
            //}
            //else
            //{
            //    textBox14.Text = "停止中";
            //}

            //////////DI顯示///////////////////////////////////////////////////////
            for (ushort i = 0; i < 16; i++)
            {
                foreach (Control ctemp in groupBox7.Controls)
                {
                    if (ctemp.Name == "textBox" + (i + 100).ToString())
                    {
                        ((System.Windows.Forms.TextBox)ctemp).Text = DIO_Thread.Di[i].ToString();
                    }
                }
            }
            /*
            textBox23.Text = DIO_Thread.Di[0].ToString();
            textBox24.Text = DIO_Thread.Di[1].ToString();
            textBox25.Text = DIO_Thread.Di[2].ToString();
            textBox26.Text = DIO_Thread.Di[3].ToString();
            textBox27.Text = DIO_Thread.Di[4].ToString();
            textBox28.Text = DIO_Thread.Di[5].ToString();
            textBox29.Text = DIO_Thread.Di[6].ToString();
            textBox30.Text = DIO_Thread.Di[7].ToString();
            textBox31.Text = DIO_Thread.Di[8].ToString();
            textBox32.Text = DIO_Thread.Di[9].ToString();
            textBox33.Text = DIO_Thread.Di[10].ToString();
            textBox34.Text = DIO_Thread.Di[11].ToString();
            textBox35.Text = DIO_Thread.Di[12].ToString();
            textBox36.Text = DIO_Thread.Di[13].ToString();
            textBox37.Text = DIO_Thread.Di[14].ToString();
            textBox38.Text = DIO_Thread.Di[15].ToString();
            */
        }

        private void Param_Form_FormClosed(object sender, FormClosedEventArgs e)
        {
            OCR_Form.param_Form = null;
        }

        private void StartRealTimeBtn_Click(object sender, EventArgs e)
        {

        }

        private void StopRealTimeBtn_Click(object sender, EventArgs e)
        {

        }

        private void SingleTriggerBtn_Click(object sender, EventArgs e)
        {

        }

        private void ModbusNgBtn_Click(object sender, EventArgs e)
        {

        }
    }
}
